#Author: Ark2000
#E-mail: Ark2000@foxmail.com
#v0.1
#Date: 2017-5-21

from random import randint,choice
from time import sleep

WIDTH = 6
HEIGHT = 6
GOAL = 2048

#initialize game board
score = 0
board = [[0 for i in range(WIDTH)] for i in range(HEIGHT)]
while True:
    [a,b,c,d] = [randint(0,WIDTH-1) for i in range(4)]
    if (a,b) != (c,d):
        break
board[a][b],board[c][d] = 2,2

def my_input(words):
    a = input(words)
    if a not in ('w','W','s','S','a','d','A','D'):
        print('invaild input')
        my_input(words)
    else:
        return a
    
#translate double list into string pattern
def board_str(board):
    list_board = board[:]
    str_board = ''
    for index in range(HEIGHT):
        list_board[index] = [str(num) for num in board[index]]
        if index != 0:
            str_board += '\n'
        str_board += '| ' + ' | '.join(list_board[index]) + ' |'
    return str_board
    
#rotate board(0:anticlockwise,1:clockwise)
def rotate_board(board,direction):
    new_board = []
    for i in range(1,WIDTH+1):
        new_board.append([line[-i] for line in board]) 
    if direction == 0:     
        return new_board  
    elif direction == 1:
        return [line[::-1] for line in new_board][::-1]

def board_add(board):
    while True:
        i,j = randint(0,HEIGHT-1),randint(0,WIDTH-1)
        if board[i][j] == 0:
            board[i][j] = choice([2,2,2,4])
            break
            
#left: 0, right: 1
def add_same(board,direction):
    global score
    d = [-1,1][direction]  
    for line in board:
        #确保index不溢出
        for i in range(int(0.5-d/2),len(line)+int(-0.5-d/2)):
            if line[i] != 0 and line[i] == line[i+d]:
                line[i+d] += line[i]
                line[i] = 0
                score += line[i+d]

def board_update(board,command):
    new_board = []
    zeros = []
    if command in ('w','W','s','S'):
        board = rotate_board(board,0)  
    for line in board:
        new_line = [num for num in line if num != 0]
        new_board.append(new_line)
        zeros.append([0 for i in range(len(line) - len(new_line))])
        
    if command in ('w','W','a','A'):
        new_board = [new_board[i] + zeros[i] for i in range(len(zeros))] 
        add_same(new_board,0)         
    else:
        new_board = [zeros[i] + new_board[i] for i in range(len(zeros))]
        add_same(new_board,1)
    if command in ('w','W','s','S'):
        new_board = rotate_board(new_board,1)
    return new_board
    
def is_win(board):
    win = 0
    zeros = 0
    for line in board:
        zeros += line.count(0)
        if GOAL in line:
            win = 1
    if zeros == 0:
        w = board_update(board,'w') == board
        s = board_update(board,'s') == board
        a = board_update(board,'a') == board
        d = board_update(board,'d') == board      
        if (w and s and a and d):
            win = -1
    return win
i = 1
while True:
    print('\n'*14)
    print('score: %d'%score)
    print(i)
    print(board_str(board))
    result = is_win(board)
    if result == 1:
        print('you win')
        break
    elif result == -1:
        print('you lose')
        break
    #command = my_input('>>>') 
    command = choice(['a','d','s','w'])
    board = board_update(board,command)
    board = board_update(board,command)
    board_add(board)
    i += 1
    
    



